package org.tis.tools.senior.module.developer.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.tis.tools.senior.module.developer.controller.request.ProjectAddAndUpdateRequest;
import org.tis.tools.senior.module.developer.dao.SProjectMapper;
import org.tis.tools.senior.module.developer.entity.SProject;
import org.tis.tools.senior.module.developer.exception.DeveloperException;
import org.tis.tools.senior.module.developer.service.ISProjectService;

import java.util.List;

/**
 * sProject的Service接口实现类
 * 
 * @author Auto Generate Tools
 * @date 2018/06/20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SProjectServiceImpl extends ServiceImpl<SProjectMapper, SProject> implements ISProjectService {


    @Override
    public List<SProject> selectProjectAll() {
        return selectList(null);
    }

    @Override
    public SProject add(ProjectAddAndUpdateRequest request) {
        List<SProject> projects =  selectProjectAll();
        for(SProject project:projects){
            if(request.getProjectName().equals(project.getProjectName())){
                throw new DeveloperException("新增的工程名已存在！");
            }
        }
        SProject sProject = new SProject();
        BeanUtils.copyProperties(request,sProject);
        insert(sProject);
        return sProject;
    }

    @Override
    public SProject update(ProjectAddAndUpdateRequest request) {
        List<SProject> projects =  selectProjectAll();
        for(SProject project:projects){
            if(request.getProjectName().equals(project.getProjectName()) && !request.getGuid().equals(project.getGuid())){
                throw new DeveloperException("修改的工程名已存在！");
            }
        }
        SProject sProject = new SProject();
        BeanUtils.copyProperties(request,sProject);
        updateById(sProject);
        return sProject;
    }

    /*@PostConstruct
    private void loadAllProject(){
        this.spList = selectList(null);
    }*/
}

